<template>
	<div class="user-profile">
		<!-- 导航栏 -->
		<van-nav-bar
			class="page-nav-bar"
			title="个人信息"
			left-arrow
			@click-left="$router.back()"
		/>
		<!-- /导航栏 -->
		<!-- input文件框 -->
		<input type="file" hidden ref="file" @change="onFileChange" />
		<!-- 个人信息 -->
		<van-cell
			class="avatar-cell"
			title="头像"
			is-link
			center
			@click="$refs.file.click()"
		>
			<van-image class="avatar" round fit="cover" :src="user.photo" />
		</van-cell>
		<van-cell
			title="昵称"
			:value="user.name"
			is-link
			@click="isUpdateNameShow = true"
		/>
		<van-cell
			title="性别"
			:value="user.gender === 0 ? '男' : '女'"
			is-link
			@click="isUpdateGenderShow = true"
		/>
		<van-cell
			title="生日"
			:value="user.birthday"
			is-link
			@click="isUpdateBirthdayShow = true"
		/>
		<!-- /个人信息 -->

		<!-- 编辑昵称 -->
		<van-popup
			v-model="isUpdateNameShow"
			style="height: 100%;"
			position="bottom"
		>
			<update-name
				@close="isUpdateNameShow = false"
				v-model="user.name"
				v-if="isUpdateNameShow"
			></update-name>
		</van-popup>
		<!-- /编辑昵称 -->

		<!-- 编辑性别 -->
		<van-popup v-model="isUpdateGenderShow" position="bottom">
			<update-gender
				@close="isUpdateGenderShow = false"
				v-model="user.gender"
				v-if="isUpdateGenderShow"
			></update-gender>
		</van-popup>
		<!-- /编辑性别 -->
		<!-- 编辑生日 -->
		<van-popup v-model="isUpdateBirthdayShow" position="bottom">
			<update-birthday
				@close="isUpdateBirthdayShow = false"
				v-if="isUpdateBirthdayShow"
				v-model="user.birthday"
			></update-birthday>
		</van-popup>
		<!-- /编辑生日 -->
		<!-- 编辑头像 -->
		<van-popup
			v-model="isUpdatePhotoShow"
			position="bottom"
			style="height: 100%;"
		>
			<update-photo
				v-if="isUpdatePhotoShow"
				:img="img"
				@close="isUpdatePhotoShow = false"
        @update-photo="user.photo=$event"
			></update-photo>
		</van-popup>
		<!-- /编辑头像 -->
	</div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'
export default {
	name: 'UserProfile',
	components: { UpdateName, UpdateGender, UpdateBirthday, UpdatePhoto },
	props: {},
	data() {
		return {
			user: {},
			isUpdateNameShow: false,
			isUpdateGenderShow: false,
			isUpdateBirthdayShow: false,
			isUpdatePhotoShow: false,
			img: null, //预览的图片
		}
	},
	computed: {},
	watch: {},
	created() {
		this.loadUserProfile()
	},
	mounted() {},
	methods: {
		async loadUserProfile() {
			try {
				const { data } = await getUserProfile()
				this.user = data.data
			} catch (err) {
				this.$toast('数据获取失败')
			}
		},
		onFileChange() {
			const file = this.$refs.file.files[0]
			this.img = window.URL.createObjectURL(file)
      console.log(this.img);//blob:http://localhost:8080/cbe9f636-5a10-4d69-8b89-7ae06f107610;这里不能使用v-model
			// 展示预览弹出层
			this.isUpdatePhotoShow = true
			// 如果是同一个图片，不会触发change，因此每次使用完成清空
			this.$refs.file.value = ''
		},
	},
}
</script>

<style scoped lang="less">
.user-profile {
	.avatar-cell {
		.van-cell__value {
			display: flex;
			flex-direction: row-reverse;
		}
		.avatar {
			width: 60px;
			height: 60px;
		}
	}
	.van-popup {
		background-color: #f5f7f9;
	}
}
</style>
